package com.ai5suoai.ams.web.handler;

import com.ai5suoai.ams.web.util.ResponseEntityUtil;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.BindException;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.web.HttpMediaTypeNotAcceptableException;
import org.springframework.web.HttpMediaTypeNotSupportedException;
import org.springframework.web.HttpRequestMethodNotSupportedException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.servlet.NoHandlerFoundException;
import org.springframework.web.servlet.mvc.multiaction.NoSuchRequestHandlingMethodException;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;

/**
 * @author chenzhenjia
 * @since 2016/3/18
 */
@ControllerAdvice
public class HttpJsonExceptionHandler {
    @Autowired
    private ResponseEntityUtil responseEntityUtil;
    public static final Logger logger = LoggerFactory.getLogger(HttpJsonExceptionHandler.class);

    @ExceptionHandler(BindException.class)
    @ResponseStatus(HttpStatus.BAD_REQUEST)
    @ResponseBody
    public ResponseEntity processValidationError(BindException ex) {
        logger.debug("processValidationError ex: ", ex);
        BindingResult result = ex.getBindingResult();
        List<FieldError> fieldErrors = result.getFieldErrors();
        Map<String, String> res = new HashMap<>();
        fieldErrors.stream().forEach(fieldError -> res.put(fieldError.getField(), fieldError.getDefaultMessage()));
        return ResponseEntity.badRequest().body(res);
    }

    @ExceptionHandler(ServletException.class)
    @ResponseBody
    public ResponseEntity processServletException(ServletException ex) {
        if (ex instanceof NoSuchRequestHandlingMethodException) {
            return responseEntityUtil.fail(HttpStatus.NOT_FOUND, ex.getMessage());
        } else if (ex instanceof HttpRequestMethodNotSupportedException) {
            return responseEntityUtil.fail(HttpStatus.METHOD_NOT_ALLOWED, ex.getMessage());
        } else if (ex instanceof HttpMediaTypeNotSupportedException) {
            return responseEntityUtil.fail(HttpStatus.UNSUPPORTED_MEDIA_TYPE, ex.getMessage());
        } else if (ex instanceof HttpMediaTypeNotAcceptableException) {
            return responseEntityUtil.fail(HttpStatus.NOT_ACCEPTABLE, ex.getMessage());
        } else if (ex instanceof NoHandlerFoundException) {
            return responseEntityUtil.fail(HttpStatus.NOT_FOUND, ex.getMessage());
        }
        return responseEntityUtil.fail(HttpStatus.BAD_REQUEST, ex.getMessage());
    }

}
